//
//  SuDishViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "SuDishViewController.h"
#import "AppDelegate.h"
#import "drawerViewController.h"
#import "SuDishViewTableViewCell.h"
#import "SuDishList.h"
#import "SuDetailViewController.h"

@interface SuDishViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation SuDishViewController
- (NSMutableArray *)arr{
    if (!_arr) {
        _arr = [NSMutableArray array];
    }
    return _arr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Click)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"SuDishViewCell" bundle:nil] forCellReuseIdentifier:@"SuDishViewCell"];
    [self getData];
}

- (void)getData{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
     [manager GET:kSuDishList parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        NSArray *array = responseObject[@"data"];
        for (NSDictionary *d in array) {
            SuDishList *model = [[SuDishList alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
   
   
    
    
    
}
//delegate && dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SuDishViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuDishViewCell" forIndexPath:indexPath];
    SuDishList *model = self.arr[indexPath.row];
    cell.model = model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SuDetailViewController *VC = [[SuDetailViewController alloc]init];
    VC.model = self.arr[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];

    
}
- (void)Click{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    drawerViewController *drawer = [delegate.VCDic objectForKey:@"drawer"];
    
    delegate.window.rootViewController = drawer;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
